package soar.template.springboot.service.hello.configuration.mvc;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import soar.template.springboot.service.hello.controller.BaseController;

@Component
public class SecurityInterceptor implements HandlerInterceptor
{
    private final static Logger logger = LoggerFactory
            .getLogger(SecurityInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception
    {
        if (handler instanceof HandlerMethod)
        { // @RequestMapping
            HandlerMethod handler_method = (HandlerMethod) handler;
            Object bean = handler_method.getBean();
            if (bean instanceof BaseController)
            {
                String method = request.getMethod();
                Map<String, String[]> paras = request.getParameterMap();
                logger.info("method({}) paras({})", method,
                        paras.entrySet().stream()
                                .map(x -> x.getKey() + "="
                                        + Stream.of(x.getValue())
                                                .collect(Collectors.joining(",")))
                                .collect(Collectors.joining("&")));
                if (method.equals("POST"))
                {
                    
                }
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception
    {
    }

    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception e)
            throws Exception
    {
    }

}
